package com.akk.platform.manage;

import com.akk.module.avs.entity.AvsApplyLoan;
import com.akk.module.avs.enums.AvsIncreEnums;
import com.akk.module.avs.model.request.AvsApplyLoanRequest;
import com.akk.module.avs.model.request.AvsLoanRequest;
import com.akk.module.avs.service.AvsApplyLoanService;
import com.akk.module.upload.service.PictureService;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.RestResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/platform/manage/loan")
public class ApplyLoanAction {

    @Autowired
    private AvsApplyLoanService loanService;

    @Autowired
    private PictureService pictureServices;

    @RequestMapping(value = "/list")
    @RequiresPermissions("sys:apply:loan:list")
    public RestResponse list(@Valid Query query, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        Map param = query.getSearch();
        PageHelper.startPage(query.getPage(),query.getLimit());
        List<AvsApplyLoan> list = loanService.queryByParams(param);
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    @RequestMapping(value = "/info/{id}")
    @RequiresPermissions("sys:apply:loan:info")
    public RestResponse info(@PathVariable Long id){
        AvsApplyLoan applyLoanEntity = loanService.queryById(id);
        if (applyLoanEntity == null)
            return RestResponse.build("1","信息不存在");
        return RestResponse.build(applyLoanEntity);
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @RequiresPermissions("sys:apply:loan:save")
    public RestResponse save(AvsLoanRequest request){
        if (request.getLoanIcon() == null || request.getLoanHeaderImage() == null){
            return RestResponse.build("1","请上传对应图标");
        }
        Map<String,Object> result1 = pictureServices.uploadPicture(request.getLoanHeaderImage());
        if (result1.get("status").toString().equals("1")){
            return RestResponse.build("1","文件上传失败");
        }
        Map<String,Object> result2 = pictureServices.uploadPicture(request.getLoanIcon());
        if (result2.get("status").toString().equals("1")){
            return RestResponse.build("1","文件上传失败");
        }
        AvsApplyLoanRequest entity = new AvsApplyLoanRequest();
        entity.setLoanDesc(request.getLoanDesc());
        entity.setLoanHeaderImage(result1.get("url").toString());
        entity.setLoanIcon(result2.get("url").toString());
        entity.setLoanMoney(request.getLoanMoney());
        entity.setLoanStatus(request.getLoanStatus());
        entity.setLoanName(request.getLoanName());
        entity.setLoanUrl(StringEscapeUtils.unescapeHtml(request.getLoanUrl()));
        entity.setLoanIndex(request.getLoanIndex());
        entity.setConsStr(request.getCons());
        loanService.save(entity);
        return RestResponse.build("success");
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @RequiresPermissions("sys:apply:loan:update")
    public RestResponse update(AvsLoanRequest request){
        if (request.getLoanId() == null){
            return RestResponse.build("1","信息不存在");
        }
        AvsApplyLoan entity = loanService.queryById(request.getLoanId());
        if (entity == null){
            return RestResponse.build("1","信息不存在");
        }
        if (request.getLoanStatus() == AvsIncreEnums.LoanStatus.RECOM.getValue())
        {
            loanService.updateStatusNomralForAll();
        }
        if (request.getLoanHeaderImage() != null){
            Map<String,Object> result1 = pictureServices.uploadPicture(request.getLoanHeaderImage());
            if (result1.get("status").toString().equals("1")){
                return RestResponse.build("1","文件上传失败");
            }
            entity.setLoanHeaderImage(result1.get("url").toString());
        }

        if (request.getLoanIcon() != null){
            Map<String,Object> result2 = pictureServices.uploadPicture(request.getLoanIcon());
            if (result2.get("status").toString().equals("1")){
                return RestResponse.build("1","文件上传失败");
            }
            entity.setLoanIcon(result2.get("url").toString());
        }
        entity.setLoanDesc(request.getLoanDesc());
        entity.setLoanId(request.getLoanId());
        entity.setLoanMoney(request.getLoanMoney());
        entity.setLoanName(request.getLoanName());
        entity.setLoanStatus(request.getLoanStatus());
        entity.setLoanIndex(request.getLoanIndex());
        entity.setLoanUrl(StringEscapeUtils.unescapeHtml(request.getLoanUrl()));
        entity.setConsStr(request.getCons());
        loanService.update(entity);
        return RestResponse.build("success");
    }

    @RequestMapping(value = "/delete/{id}")
    @RequiresPermissions("sys:apply:loan:delete")
    public RestResponse delete(@PathVariable Long id){
        loanService.delete(id);
        return RestResponse.build("success");
    }

}
